﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Newtonsoft.Json;
namespace BallData
{
    public class testjson
    {
        public DateTime t1 { get; set; }
        public String t2{get;set;}
    }
    public class OddsInfo_d
    {
        public String mId { get; set; }
        public String cId { get; set; }
        public String oType { get; set; }
        public String sId { get; set; }
        public int chgTimeUTC { get; set; }
        public DateTime chgTime { get; set; }
        public String host { get; set; }
        public String vs { get; set; }
        public String visit { get; set; }
        public String period { get; set; }
        public String vnname{get;set;}
        public String updatetime{get;set;}
        public String oddsdata1 { get; set; }
        public String oddsdata2 { get; set; }
        public String oddsdata3 { get; set; }
        public String allodds { get;set; }
        public String getmoneycent { get; set; }

        public OddsInfo_d()
        {

        }
        public OddsInfo_d(String matchoddsdata)
        {
            String[] infoArr = matchoddsdata.Split(OddsUntil.SplitColumn);

            this.cId = infoArr[0];
            this.sId = infoArr[1];
            this.chgTimeUTC = Convert.ToInt32(infoArr[2], 10);
            this.chgTime = OddsUntil.ConvertIntDateTime(this.chgTimeUTC);
            
            this.host = infoArr[3];
            this.vs = infoArr[4];
            this.visit = infoArr[5];
            this.period = infoArr[6];
            this.oType = infoArr[8];
            this.mId = infoArr[9];
            this.updatetime = this.chgTime.ToString("yyyy-MM-dd hh:mm:ss");
            
            this.vnname = GetVsName();
            this.period = OddsUntil.getPeriod(this.period, false);

            if (oType == "a")
            {
                this.host = Convert.ToString(Convert.ToDouble(this.host) - 1);
                this.visit = Convert.ToString(Convert.ToDouble(this.visit) - 1);
                
                allodds = Convert.ToString(Convert.ToDouble(this.host) + Convert.ToDouble(this.visit));

                getmoneycent = String.Format("{0:P}", (2 - Convert.ToDouble(this.host) - Convert.ToDouble(this.visit))/2);
            }

            if (oType == "e")
            {
                oddsdata1 = String.Format("[{0:00.00}],[{1:00.00}],[{2:00.00}]", Convert.ToDouble(this.host), Convert.ToDouble(this.vs), Convert.ToDouble(this.visit));
                Hashtable ret = OddsUntil.getEuropeRate(Convert.ToDouble(this.host), Convert.ToDouble(this.vs), Convert.ToDouble(this.visit));
                oddsdata2 = (String)ret["retRate"];
                oddsdata3 = String.Format("[{0}],[{1}],[{2}]", (String)ret["winRate"], (String)ret["drawRate"], (String)ret["loseRate"]);

            }
            else
            {
                oddsdata1 = String.Empty;
                oddsdata2 = String.Empty;
                oddsdata3 = String.Empty;
            }
        }

        public String GetVsName()
        {
            String ret = "";
            String vid = vs;
            if (this.oType == "a")
            {
                ret = OddsUntil.GetVsName(vid, "tr");
            }
            else if (this.oType == "o")
            {
                ret = OddsUntil.GetTotalName(vid);
            }
            return ret;
            
        }

        public static String toJson(OddsInfo_d odds)
        {
            String json = JsonConvert.SerializeObject(odds);

            return json;

        }
        public static OddsInfo_d fromJson(String jsondata)
        {
            OddsInfo_d oddsinfo = JsonConvert.DeserializeObject<OddsInfo_d>(jsondata);

            oddsinfo.chgTime = OddsUntil.ConvertIntDateTime(oddsinfo.chgTimeUTC);
            oddsinfo.updatetime = oddsinfo.chgTime.ToString("yyyy-MM-dd hh:mm:ss");
            oddsinfo.vnname = oddsinfo.GetVsName();

            if (oddsinfo.oType == "e")
            {
                oddsinfo.oddsdata1 = String.Format("[{0:00.00}],[{1:00.00}],[{2:00.00}]", Convert.ToDouble(oddsinfo.host), Convert.ToDouble(oddsinfo.vs), Convert.ToDouble(oddsinfo.visit));
                Hashtable ret = OddsUntil.getEuropeRate(Convert.ToDouble(oddsinfo.host), Convert.ToDouble(oddsinfo.vs), Convert.ToDouble(oddsinfo.visit));
                oddsinfo.oddsdata2 = (String)ret["retRate"];
                oddsinfo.oddsdata3 = String.Format("[{0}],[{1}],[{2}]", (String)ret["winRate"], (String)ret["drawRate"], (String)ret["loseRate"]);

            }
            else
            {
                oddsinfo.oddsdata1 = String.Empty;
                oddsinfo.oddsdata2 = String.Empty;
                oddsinfo.oddsdata3 = String.Empty;
            }

            return oddsinfo;
        }
    }
}
